//
//  EmotionKeyboardController.swift
//  MyWeiBo
//
//  Created by mac on 15/3/11.
//  Copyright (c) 2015年 gpr. All rights reserved.
//

import UIKit

protocol EmotionKeyboardDelegate: NSObjectProtocol {
    func emotionKeyboardDidSelected(emoticon: Emoticon)
}

class EmotionKeyboardController: UIViewController {

    class func emotionKeyboardController() -> EmotionKeyboardController {
        return UIStoryboard(name: "EmotionKeyboardController", bundle: nil).instantiateInitialViewController() as! EmotionKeyboardController
    }
    
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewLayout: UICollectionViewFlowLayout!
    var emoticonGroups: [EmoticonsGroup]?
    weak var delegate: EmotionKeyboardDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpEmoticonDatas()
    }
    
    func setUpEmoticonDatas() {
        emoticonGroups = EmoticonsTool.loadEmotionsGroup()
    }

    var i = 0
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        setUpCollectionView()

    }
    
    // MARK: 初始化 collectionView
    func setUpCollectionView() {
        let row: CGFloat = 3
        let column: CGFloat = 7
        let margin: CGFloat = 10
        let inset = 0.5 * margin
        let collectionViewW = collectionView.bounds.size.width
        let cellWH = (collectionViewW - (column - 1) * margin - 2 *  inset) / column
        collectionViewLayout.minimumInteritemSpacing = margin
        collectionViewLayout.minimumLineSpacing = margin
        collectionViewLayout.itemSize = CGSizeMake(cellWH, cellWH)
        collectionViewLayout.sectionInset = UIEdgeInsetsMake( inset , inset , inset, inset )
        collectionViewLayout.scrollDirection = .Horizontal
        collectionView.pagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
    }
}

extension EmotionKeyboardController: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return emoticonGroups?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let emoticons = emoticonGroups![section]
        return emoticons.emotions?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = EmotionKeyboardCell.emotionKeyboardCellWithCollectionView(collectionView, indexPath: indexPath)
        let group = emoticonGroups![indexPath.section]
        let emoticon = group.emotions![indexPath.item]
        cell.emotion = emoticon
        return cell
    }
    
    
    // MARK: UICollectionViewDelegate
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if delegate != nil {
            let group = emoticonGroups![indexPath.section]
            let emoticon = group.emotions![indexPath.item]
            if emoticon.isNone { return }
            delegate?.emotionKeyboardDidSelected(emoticon)
        }
    }
}
